{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__main__\n",
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "   WARNING: This is a development server. Do not use it in a production deployment.\n",
      "   Use a production WSGI server instead.\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [07/May/2022 09:19:23] \"GET / HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [07/May/2022 09:19:31] \"GET / HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [07/May/2022 09:19:43] \"GET /test/www HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [07/May/2022 09:20:03] \"GET /flask/内蒙古 HTTP/1.1\" 200 -\n"
     ]
    }
   ],
   "source": [
    "#1. 导入flask\n",
    "from flask import Flask \n",
    "from flask import request\n",
    "from flask import render_template\n",
    "\n",
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "#2. 创建flask应用实例\n",
    "app = Flask(__name__)  \n",
    "\n",
    "print(__name__)\n",
    "\n",
    "#3.练习1.jinja2模板完成A部分第二个动态内容的效果，Jupyter上运行，templates文件放在桌面\n",
    "@app.route('/test/<name>') \n",
    "def test(name):\n",
    "    name = name + \"！！！\"\n",
    "    return render_template(\"xxx.html\", city = name)\n",
    "\n",
    "#4.练习2.读取gdp.csv数据，自行实现一个视图函数以及模板\n",
    "@app.route('/flask/<name>') #输入名字对应gdp的名字\n",
    "def gdp(name):\n",
    "    df = pd.read_csv(r\"C:\\Users\\01\\Desktop\\gdp.csv\")\n",
    "    province_gdp = df[df[\"province\"] == name]\n",
    "    if len(province_gdp) != 0:\n",
    "        cg = df[df[\"province\"] == name].iloc[0, 1:]\n",
    "        gdp_years = [{ \"year\": year, \"gdp\":gdp  }  for year,gdp in zip(cg.index, cg.values)]\n",
    "    else:\n",
    "        gdp_years = []\n",
    "    return render_template(\"yyy.html\", province = name, gdp_province = gdp_years)\n",
    "\n",
    "#5. 启动主函数，\n",
    "if __name__ == '__main__':  \n",
    "    app.run()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
